package com.example.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/test")
@RestController
public class Demo1 {

    @RequestMapping("/t1")
    public String test1(String keyword) {

        return "返回参数：" + keyword;

    }

    @RequestMapping("/t2")
    public String test2(String name, String password) {

        return "用户名：" + name + " " + "密码:" + password;
    }

    @RequestMapping("/t3")
    public String test3(Integer i) {
        return "返回参数：" + i;

    }

    @RequestMapping("t4")
    public String test4(int i) {
        return "返回参数：" + i;
    }

    @RequestMapping("t5")
    public String test5(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/t6")
    public String test6(@RequestParam(value = "q", required = false) String k) {
        return k;
    }

    @RequestMapping("/t7")
    public String test7(@RequestParam("q") String k) {
        return k;
    }

    @RequestMapping("/t8")
    public String test8(@RequestParam List<String> list) {
        return list.toString();
    }

    @RequestMapping("/t9")
    public String test9(String[] arr) {
        return Arrays.toString(arr);
    }

    @RequestMapping("/t10/{artId}")
    public String test10(@PathVariable String artId) {
        return artId;
    }

    @RequestMapping("/t11/{type}/{artId}")
    public String test11(@PathVariable String artId, @PathVariable String type) {
        return type + " " + artId;
    }

    @RequestMapping("/t12/{type}/{artId}")
    public String test12(@PathVariable("type") String t, @PathVariable String artId) {
        return t + " " + artId;
    }

    @RequestMapping("/t13")
    public String test13(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\xian\\" + file.getOriginalFilename()));
        return "上传成功";
    }

    @RequestMapping("/t14")
    public String test14(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\xian\\" + file.getOriginalFilename()));
        return "上传成功";
    }

    @RequestMapping("/t15")
    public String test15(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\xian\\" + file.getOriginalFilename()));
        return "上传成功";
    }

    @RequestMapping("/t16")
    public String test16(@RequestPart("file11") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\xian\\" + file.getOriginalFilename()));
        return "上传成功";
    }

    @RequestMapping("/t17")
    public String test17(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println("key:" + cookie.getName() + " " + "value:" + cookie.getValue());

            }

        }
        return "获取成功";
    }

    @RequestMapping("/18")
    public String test18(@CookieValue("java") String java) {
        return java;
    }

    @RequestMapping("/19")
    public String test19(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("usename", "zhangsan");
        session.setAttribute("age", 23);
        return "设置成功";
    }

    @RequestMapping("/20")
    public String test20(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            return "用户未登录";
        } else {
            String s = (String) session.getAttribute("usename");
            return "登录名：" + s;
        }

    }

    @RequestMapping("/21")
    public String test21(HttpSession session) {
        String name = (String) session.getAttribute("name");
        return name;
    }

    @RequestMapping("/22")
    public String test22(@SessionAttribute("name") String s) {
        return s;
    }


    @RequestMapping("/t23")
    public String test23(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String s = (String) session.getAttribute("name");
        return s;
    }

    @RequestMapping("/t24")
    public String test24(HttpSession session) {
        String attribute = (String) session.getAttribute("name");
        return attribute;
    }

    @RequestMapping("/t25")
    public String test25(@SessionAttribute("name") String name) {
        return name;
    }

    @RequestMapping("/t26")
    public String test26(HttpServletRequest request) {
        String s = request.getHeader("User-Agent");
        return s;
    }

    @RequestMapping("/t27")
    public String test27(@RequestHeader("User-Agent") String s) {
        return s;
    }

    @RequestMapping("/t28")
    public String test28(HttpServletRequest request) {

        String header = request.getHeader("User-Agent");
        return header;
    }

    @RequestMapping("/t29")
    public String test29(@RequestHeader("User-Agent") String s) {
        return s;
    }


}
